﻿<%@ WebHandler Language="C#" Class="ZSToolRegularCheck" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 装配试车厂工装工具定检查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2022.11.24
 **/

public class ZSToolRegularCheck : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getQuerySql(JObject jObject)
    {
        string sql = @"SELECT * FROM t_toolregularcheckinfo t WHERE 1=1 ";

        //物料描述
        if (jObject["maktx"] != null && jObject["maktx"].ToString() != "")
        {
            sql += string.Format(" AND t.maktx LIKE '%{0}%'", jObject["maktx"].ToString());
        }

        //图号
        if (jObject["zeinr"] != null && jObject["zeinr"].ToString() != "")
        {
            sql += string.Format(" AND t.zeinr LIKE '%{0}%'", jObject["zeinr"].ToString());
        }

        //送检类别
        if (jObject["chktyp"] != null && jObject["chktyp"].ToString() != "")
        {
            sql += string.Format(" AND t.chktyp LIKE '%{0}%'", jObject["chktyp"].ToString());
        }

        //成本中心
        if (jObject["kostl"] != null && jObject["kostl"].ToString() != "")
        {
            sql += string.Format(" AND t.kostl LIKE '%{0}%'", jObject["kostl"].ToString());
        }
        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        DataTable dt = dal.GetDataTable(sql);
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        dt.Columns.Remove("status");
        dt.Columns.Remove("productfamilyid");
        dt.Columns["matnr"].Caption = "物料";
        dt.Columns["sernr"].Caption = "工装工具编号";
        dt.Columns["glstatus"].Caption = "管理状态";
        dt.Columns["vrprc"].Caption = "送检过程";
        dt.Columns["chktyp"].Caption = "送检类别";
        dt.Columns["vldat"].Caption = "有效日期";
        dt.Columns["prueflos"].Caption = "检验批号";
        dt.Columns["passed"].Caption = "检定结果";
        dt.Columns["menge"].Caption = "数量";
        dt.Columns["kostl"].Caption = "成本中心";
        dt.Columns["maktx"].Caption = "物料描述";
        dt.Columns["lgort"].Caption = "库位";
        dt.Columns["ckgrp"].Caption = "检定机构";
        dt.Columns["zxhao"].Caption = "型号";
        dt.Columns["zeinr"].Caption = "图号";
        dt.Columns["zjdzq"].Caption = "检定周期";
        dt.Columns["labst"].Caption = "可用数量";
        dt.Columns["ckusr"].Caption = "检定员";
        dt.Columns["cknam"].Caption = "检定员全名";
        dt.Columns["zposition"].Caption = "存放位置";
        dt.Columns["productfamilyname"].Caption = "机型";
        ExportHelper.ExportExcel(dt, jObject["factoryname"].ToString() + "工装工具定检信息查询报表");
    }
}